package main

import (
	"context"
	"fmt"
	stdlog "log"
	"net"

	"distributed/log"
	"distributed/portal"
	"distributed/registry"
	"distributed/service"
)

func main() {
	err := portal.ImportTemplates()
	if err != nil {
		stdlog.Fatal(err)
	}
	host, port := "localhost", "5000"
	addr := net.JoinHostPort(host, port)
	serviceAddr := fmt.Sprintf("http://%s", addr)

	r := registry.Registration{
		ServiceName: registry.PortalService,
		ServiceURL:  serviceAddr,
		RequiredServices: []registry.ServiceName{
			registry.LogService,
			registry.GradingService,
		},
		ServiceUpdateURL: serviceAddr + "services",
		BeartbeatURL:     serviceAddr + "beartbeat",
	}

	ctx, err := service.Start(
		context.Background(), r, addr, portal.RegisterHandlers,
	)
	if err != nil {
		stdlog.Fatal(err)
	}
	logService, err := registry.GetProviders(registry.LogService)
	if err == nil {
		log.SetClientLogger(logService[0], r.ServiceName)
	}
	<-ctx.Done()
	fmt.Println("Shutting down portal.")
}
